---@diagnostic disable: lowercase-global, discard-returns

local Strs = {}
local function split( str,reps )
    local resultStrList = {}
    string.gsub(str,'[^'..reps..']+',function ( w )
        table.insert(resultStrList,w)
    end)
    return resultStrList
end

local function XG_Sub(str,left,right)
	if left > right then
		return ''
	end
	return str:sub(left,right)
end

function XG_StrFormat_Reg_Lua(name,code,f)
	name = name:sub(2,-2)
	if code:sub(1,1) == '"' then
		code = code:sub(2, -2)
	end

	Strs[name] = {
		code = code,
		variable = f
	}
	--log.debug(  'str_reg', name, code )
	return 'DoNothing()'
end

function XG_StrFormat_withParams(s,a)
	local str,p = {},1
	local q,f = '',0
	for i=1,#a do
		s=s:gsub('([^\\])(\\n)(.)',
			function(x,w,y)
				if x==y and x=='"' then
					return '"\\n"'
				end
				return x..'"+'..'"\\n"'..'+"'..y
			end
		)

		s=s:gsub('\\\\n','\\n')
		local m,_=s:find("%%s")
		if m==nil then
			break
		end

		if m <= p then
			q = ''
		else
			q = XG_Sub(s,p,m-1)
		end

		if Strs[a[i]] == nil then
            Strs[a[i]] = {
				code = a[i],
				variable = false,
            }
            --return '."雪月： '..a[i]..' 未绑定字符串！"'
		end

		if Strs[a[i]].variable then --连接

			if f==2 then --上次 常量
				table.insert(str,q)
				table.insert(str,'"')
			elseif f==1 and q~='' then  --上次 变量
				table.insert(str,'+"')
				table.insert(str,q)
				table.insert(str,'"')
			else

			end

			if f ~= 0 then
			  table.insert(str,'+')
			elseif q~='' then
			  table.insert(str,'"')
			  table.insert(str,q)
			  table.insert(str,'"+')
			end

			table.insert( str, Strs[ a[i] ].code )
			f = 1
			s = s:sub( m + 2 )
		else --格式化
		   if f==1 then --上次 变量
		     table.insert(str,'+"')
		   elseif f==0 then
		     table.insert(str,'"')
		   end
			table.insert(str,q)
			table.insert(str,Strs[a[i]].code)
			f = 2
			s=s:sub(m+2)
		end
	end
	if f == 2 then
        s = s..'"'
    elseif f==1 then
        s = '+"'..s..'"'
    else
        s = '"'..s..'"'
    end
    table.insert(str, s)
	return table.concat(str)
end
function XG_StrFormat_Do_Lua(s,c)
	c = c:sub(2,-2)
	s = s:sub(2,-2)
	local a,b=split(c,","),''
	for i=1,#a do
		if a[i]==nil then
			b='."雪月：参数'.. i ..'错误！"'
			break
		end
	end
	if #a==0 then
		b='."雪月：未传入参数！"'
	end
	return b .. XG_StrFormat_withParams(s,a)
	--[[return b..'"'..load('return string.format("'..s..'",'..table.concat(a,',')..')')()..'"']]
end

---替换换行符
---@param str string
---@param variable any
local function changeLine( str, variable )
	if variable then
		return str
	end

	local pos = str:find('\\n', 1, true)
	
	local last = ''
	while pos do
		local chr = str:sub(pos-3,pos-1)
		local newstr = ''

		if chr == '\\\\\\' then
			goto next
		end

		if pos == 2 and chr:sub(1,1) == '"' then
			--前段为变量
			newstr = ' + "\\n" + '
		else
			--前段为常量
			newstr = '" + "\\n" + "'
		end

		str = str:sub(1,pos-2) .. newstr .. str:sub(pos+2)

		pos = pos +  (#newstr - 2)

		::next::
		pos = str:find('\\n', pos+1, true)
	end

	return str

end
--一体式  玩家 ${p} 获得了 ${gold} 金币
function XG_StrFormat_AIO_Lua(str)
	str = str:sub(2,-2)
	local i,strLen = 1,str:len()

	local rtn,count = {},0
	local start
	local name = ''

	local case
	local first = true
	while i <= strLen do
		local chr = str:sub(i,i)

		if start then
			if chr ~= '}' then
				name = name .. chr
			else
				count = count + 1
				rtn[count] = {
					name = name,
					variable = Strs[name] and Strs[name].variable or false,
					format = true,
				 }
				start = false
				name = ''
			end

			goto next
		end

		if chr == '$' then
			if str:sub(i+1,i+1) == '{' then
				i = i + 1
				start = true
				if name ~= '' then
					count = count + 1
					rtn[count] = {
						name = name,
						variable = false,
					}
					name = ''
				end
				goto next
			end
		end

		name = name .. chr
		if i >= strLen then
			count = count + 1
			rtn[count] = {
				name = name,
				variable = false,
			}
		end
		::next::
		i=i+1
	end

	local variable
	for index, value in ipairs(rtn) do
		local newvariable = value.variable
		name = value.name

		if variable then
			case = 0 + (newvariable and 0 or 1) -- 0：变量+变量 1: 变量+常量
		else
			case = 1 + (newvariable and 1 or 2) --2：常量+变量 3.常量+常量
		end

		if case == 0 then	--0：变量+变量
			rtn[index] = ' + ' .. (Strs[name] and Strs[name].code or '"'..name..'"')
		elseif case == 1 then -- 1: 变量+常量
			rtn[index] = ' + "' .. name
		elseif case == 2 then --2：常量+变量

			if index == 1 then
				rtn[index] = (Strs[name] and Strs[name].code or '"'..name..'"')
			else
				rtn[index] = '" + ' .. (Strs[name] and Strs[name].code or '"'..name..'"')
			end
			
		elseif case == 3 then	-- 3.常量+常量
			--log.debug( 'const+const', name,  newvariable)
			if index == 1 then
				rtn[index] = '"' .. (value.format and Strs[name].code or name)
			else
				rtn[index] = (value.format and (Strs[name] and Strs[name].code or name) or name )
			end
		end

		rtn[index] = changeLine(rtn[index], newvariable)
		
		variable = newvariable
	end
	if not variable then
		count = count + 1
			rtn[count] = '"'
	end
	return table.concat(rtn)
end